<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="企业名称/法定代表人/所属行业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="经营状态">
        <el-select v-model="queryParams.openState" clearable>
          <el-option
            v-for="dict in dict.type.management_forms"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="所属地区">
        <el-select v-model="queryParams.province" clearable>
          <el-option v-for="dict in dict.type.affiliating_area" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>

      <el-form-item label="录入时间" prop="registerDate">
        <el-date-picker clearable
                        v-model="queryParams.beginInputTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择开始日期">
        </el-date-picker>
        <span>-</span>
        <el-date-picker clearable
                        v-model="queryParams.endInputTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['customer:customerInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['customer:customerInfo:edit']"
        >修改</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"  :data="customerInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50"/>
      <el-table-column label="企业名称" align="center" prop="customerName" />
      <el-table-column label="法定代表人" align="center" prop="legalLeader" />
      <el-table-column label="成立日期" align="center" prop="registerDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属行业" align="center" prop="industry" />
      <el-table-column label="所属地区" align="center" prop="province" >
        <template v-slot="scope">
          <dict-tag :options="dict.type.affiliating_area" :value="scope.row.province"/>
        </template>
      </el-table-column>
      <el-table-column label="经营状态" align="center" prop="openState" >
        <template v-slot="scope">
          <dict-tag :options="dict.type.management_forms" :value="scope.row.openState"/>
        </template>
      </el-table-column>
      <el-table-column label="录入人" align="center" prop="inputUser" />
      <el-table-column label="营销人" align="center" prop="inputUser" />
      <el-table-column label="录入时间" align="center" prop="inputTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['customer:customerInfo:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-question"
            @click="handleDetails(scope.row)"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <el-row>
          <el-col :span="12" :gutter="40">
              <el-form-item label="企业名称" prop="customerName">
              <el-input v-model="form.customerName" placeholder="请输入企业名称" maxlength="100" show-word-limit />
              </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法定代表人" prop="legalLeader">
              <el-input v-model="form.legalLeader" placeholder="请输入法定代表人" maxlength="30" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" :gutter="40">
            <el-form-item label="成立日期" prop="registerDate">
              <el-date-picker clearable
                              v-model="form.registerDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择成立日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经营状态">
              <el-select v-model="form.openState" placeholder="请选择经营状态">
                  <el-option
                    v-for="dict in dict.type.management_forms"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="所属地区">
              <el-select v-model="form.province" clearable>
                <el-option
                  v-for="dict in dict.type.affiliating_area"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属行业" prop="industry">
              <el-input v-model="form.industry" placeholder="请输入所属行业" maxlength="30" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="注册资本" prop="regCapital">
              <el-input v-model="form.regCapital" type="number" placeholder="请输入金额" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="经营范围" prop="scope">
              <el-input v-model="form.scope" type="textarea" placeholder="请输入内容" maxlength="500" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="注册地址" prop="regAddr">
              <el-input v-model="form.regAddr" type="textarea" placeholder="请输入内容" maxlength="500" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--当前客户详情-->
    <el-dialog :title="title" :visible.sync="openDetails" width="800px" append-to-body>
      <el-descriptions title="客户信息" :column="number">
        <el-descriptions-item label="客户id">
          {{customer.id}}
        </el-descriptions-item>
        <el-descriptions-item label="企业名称">
          {{customer.customerName}}
        </el-descriptions-item>
        <el-descriptions-item label="法定代表人">
          {{customer.legalLeader}}
        </el-descriptions-item>
        <el-descriptions-item label="成立日期">
          <span>{{ parseTime(customer.registerDate, '{y}-{m}-{d}') }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="所属行业">
          {{customer.industry}}
        </el-descriptions-item>
        <el-descriptions-item label="所属地区">
          <dict-tag :options="dict.type.affiliating_area" :value="customer.province"/>
        </el-descriptions-item>
        <el-descriptions-item label="经营状态">
          <dict-tag :options="dict.type.management_forms" :value="customer.openState"/>
        </el-descriptions-item>
        <el-descriptions-item label="录入人">
          <span>
             {{customer.inputUser}}
          </span>
        </el-descriptions-item>
        <el-descriptions-item label="营销人">
          {{customer.inputUser}}
        </el-descriptions-item>
        <el-descriptions-item label="录入时间">
          <span>{{ parseTime(customer.inputTime, '{y}-{m}-{d}') }}</span>
        </el-descriptions-item>
      </el-descriptions>

      <template v-for="lin in linkmane">
        <el-descriptions title="联系人" >
          <el-descriptions-item label="联系人名字">
            {{lin.linkman}}
          </el-descriptions-item>
          <el-descriptions-item label="性别">
            <dict-tag :options="dict.type.sys_user_sex" :value="lin.gender"/>
          </el-descriptions-item>
          <el-descriptions-item label="年龄">
            {{lin.age}}
          </el-descriptions-item>
          <el-descriptions-item label="电话">
            {{lin.phone}}
          </el-descriptions-item>
          <el-descriptions-item label="职位">
            {{lin.position}}
          </el-descriptions-item>
          <el-descriptions-item label="部门">
            {{lin.department}}
          </el-descriptions-item>
          <el-descriptions-item label="任职状态">
            <dict-tag :options="dict.type.status_of_employment" :value="lin.positionState"/>
          </el-descriptions-item>
          <el-descriptions-item label="录入人">
            {{lin.inputUser}}
          </el-descriptions-item>
          <el-descriptions-item label="录入时间">
            {{lin.inputTime}}
          </el-descriptions-item>
        </el-descriptions>
      </template>

      <template v-for="v in visit" >
        <el-descriptions title="拜访记录" :column="number">
          <el-descriptions-item label="拜访原因">
            {{v.visitReason}}
          </el-descriptions-item>
          <el-descriptions-item label="拜访方式">
            <dict-tag :options="dict.type.access_mode" :value="v.visitType"/>
          </el-descriptions-item>
          <el-descriptions-item label="拜访日期">
            <span>{{ parseTime(v.visitDate, '{y}-{m}-{d}') }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="交流情况">
            {{v.content}}
          </el-descriptions-item>
          <el-descriptions-item label="录入人">
            {{v.inputUser}}
          </el-descriptions-item>
          <el-descriptions-item label="录入时间">
            <span>{{ parseTime(v.inputTime, '{y}-{m}-{d}') }}</span>
          </el-descriptions-item>
        </el-descriptions>
      </template>

      <template v-for="con in contract">
        <el-descriptions title="合同信息" :column="number">
          <el-descriptions-item label="合同名称">
            {{con.contractName}}
          </el-descriptions-item>
          <el-descriptions-item label="合同编号">
            {{con.contractCode}}
          </el-descriptions-item>
          <el-descriptions-item label="合同金额">
            {{con.amounts}}
          </el-descriptions-item>
          <el-descriptions-item label="合同开始日期">
            <span>{{ parseTime(con.startDate, '{y}-{m}-{d}') }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="合同结束日期">
            <span>{{ parseTime(con.endDate, '{y}-{m}-{d}') }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="电子附件">
            <el-button size="mini" type="warning"  icon="el-icon-download" plain @click="fileDownload(con.appendix)">下载</el-button>
          </el-descriptions-item>
          <el-descriptions-item label="录入人">
            {{con.inputUser}}
          </el-descriptions-item>
          <el-descriptions-item label="录入时间">
            <span>{{ parseTime(con.inputTime, '{y}-{m}-{d}') }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="审核状态">
            <dict-tag :options="dict.type.contract_audit_state" :value="con.auditState"/>
          </el-descriptions-item>
          <el-descriptions-item label="是否盖章">
            <dict-tag :options="dict.type.affix_seal_state" :value="con.affixSealState"/>
          </el-descriptions-item>
          <el-descriptions-item label="是否作废">
            <dict-tag :options="dict.type.nullify_state" :value="con.nullifyState"/>
          </el-descriptions-item>
        </el-descriptions>
      </template>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">关  闭</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listCustomerInfo, getCustomerInfo, addCustomerInfo, updateCustomerInfo } from "@/api/customer/customerInfo";
import {customerLinkmaneByCustomerId} from "../../../api/customer/customerLinkmane";
import {customerVisitByCustomerId} from "../../../api/customer/customerVisit";
import {contractByCustomerId} from "../../../api/customer/contract";
import {download} from "../../../utils/request";

export default {
  name: "CustomerInfo",
  dicts:["management_forms","access_mode","affiliating_area","sys_user_sex","status_of_employment","contract_audit_state","affix_seal_state","nullify_state"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户信息表格数据
      customerInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //
      openDetails:false,
      number:4,
      // 注册地址时间范围
      daterangeInputTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openState: null,
        province: null,
        beginInputTime: null,
        endInputTime: null,
        keyword:null,
      },
      // 表单参数
      form: {},
      //客户详情
      customer: {},
      //联系人详情
      linkmane: {},
      //拜访信息详情
      visit: {},
      //拜访信息详情
      contract: {},
      // 表单校验
      rules: {
        customerName: [
          { required: true, message: '请输入企业名称', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在100个字符以内', trigger: 'blur' }
        ],
        legalLeader: [
          { required: true, message: '请输入企业名称', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在30字符以内', trigger: 'blur' }
        ],
        registerDate: [
          { required: true, message: '请选择成立日期', trigger: 'change' }
        ],
        openState: [
          { required: true, message: '请选择经营状态', trigger: 'change' }
        ],
        province: [
          { required: true, message: '请选择所属地区', trigger: 'change' }
        ],
        industry: [
          { required: true, message: '请输入所属行业', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在30字符以内', trigger: 'blur' }
        ],
        regCapital: [
          { required: true, message: '请输入注册资本', trigger: 'blur' },
        ],
        scope: [
          { required: true, message: '请输入经营范围', trigger: 'blur' },
          { min: 1, max: 500, message: '长度在500字符以内', trigger: 'blur' }
        ],
        regAddr: [
          { required: true, message: '请输入注册地址', trigger: 'blur' },
          { min: 1, max: 500, message: '长度在500字符以内', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeInputTime && '' != this.daterangeInputTime) {
        this.queryParams.params["beginInputTime"] = this.daterangeInputTime[0];
        this.queryParams.params["endInputTime"] = this.daterangeInputTime[1];
      }
      listCustomerInfo(this.queryParams).then(response => {
        this.customerInfoList = response.rows;
        this.total = response.total;

        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerName: null,
        legalLeader: null,
        registerDate: null,
        openState: 0,
        province: null,
        regCapital: null,
        industry: null,
        scope: null,
        regAddr: null,
        inputTime: null,
        inputUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeInputTime = [];
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        openState: null,
        province: null,
        beginInputTime: null,
        endInputTime: null,
        keyword:null,
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户信息";
    },
    /** 详情按钮 */
    handleDetails(row) {
      this.customer = {}
      this.linkmane = {}
      this.visit = {}
      this.contract = {}

      getCustomerInfo(row.id).then(response => {
        this.customer = response.data;

        customerLinkmaneByCustomerId(this.customer.id).then(response => {
          this.linkmane = response.data;
        });

        customerVisitByCustomerId(this.customer.id).then(response => {
          this.visit = response.data;
        });

        contractByCustomerId(this.customer.id).then(response => {
          this.contract = response.data;
        });
        this.openDetails = true
      });
    },

    /** 详情关闭按钮 */
    handleCancel() {
      this.openDetails = false
    },
    /** 文件下载 */
    fileDownload(appendix){
      let url = appendix
      if (url == null) {
        this.$modal.msgError("当前没有文件可以下载!")
      }
      let filename = url.substring(url.lastIndexOf('/') + 1)
      download('/common/minio/download',
        { fileName: url }, filename)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomerInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomerInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomerInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
